//
//  bbbViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/1/6.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "bbbViewController.h"

@interface bbbViewController () <UIGestureRecognizerDelegate, UITextFieldDelegate>
@property (nonatomic, retain) UITextField *text;

@property (nonatomic,retain) UITextField *input;

@property (nonatomic,retain) UITextField *password;

@property (nonatomic,retain) UITextField *check;

@property (nonatomic,retain) UIButton *next;

@property (nonatomic,retain) UIButton *get;

@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, assign) NSInteger seconds;

@property (nonatomic, retain) NSMutableArray *registerArr;
@end

@implementation bbbViewController

//懒加载

- (NSMutableArray *)registerArr
{
    if (nil == _registerArr) {
        
        self.registerArr = [NSMutableArray array];
    }
    return _registerArr;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    [self.text resignFirstResponder];
    [self.input resignFirstResponder];
    [self.password resignFirstResponder];
    [self.check resignFirstResponder];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.text resignFirstResponder];
    [self.input resignFirstResponder];
    [self.password resignFirstResponder];
    [self.check resignFirstResponder];
    self.text.clearsOnInsertion = YES;
    //设置跳转下一界面清空输入框内容
    [self.text setText:nil];
    [self.input setText:nil];
    [self.password setText:nil];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    //状态栏
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0  blue:140 / 255.0  alpha:1.0];
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"but_an.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    //设置title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0],NSForegroundColorAttributeName,nil]];
    
    
    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(20 * ScreenWidth, 40 * ScreenHeigth, 60 * ScreenWidth, 45 * ScreenHeigth)];
    view.image = [UIImage imageNamed:@"dr_sr_rec05.png"];
    [self.view addSubview:view];
    
    //手机
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(20 * ScreenWidth, 40 * ScreenHeigth, 60 * ScreenWidth, 45 * ScreenHeigth)];
    la.text = @"+86";
    la.textColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    la.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:la];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(la.frame.origin.x + 60 * ScreenWidth, la.frame.origin.y, [UIScreen mainScreen].bounds.size.width - 100 * ScreenHeigth, 45 * ScreenHeigth)];
    image.image = [UIImage imageNamed:@"图层-12@2x.png"];
    [self.view addSubview:image];
    
    
    self.text = [[UITextField alloc] initWithFrame:CGRectMake(la.frame.origin.x + la.frame.size.width, la.frame.origin.y, [UIScreen mainScreen].bounds.size.width - 100 * ScreenWidth, 45 * ScreenHeigth)];
    self.text.clearButtonMode = UITextFieldViewModeAlways;
    self.text.clearsOnBeginEditing = YES;
    self.text.delegate = self;
    self.text.textColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
    self.text.keyboardType = UIKeyboardTypePhonePad;
    self.text.placeholder = @"手机号码";
    [self.view addSubview:self.text];
    
    
    //请输入验证码
    self.input = [[UITextField alloc] initWithFrame:CGRectMake(20 * ScreenWidth , self.text.frame.origin.y + self.text.frame.size.height + 20 * ScreenHeigth,  220 * ScreenWidth, 45 * ScreenHeigth)];
    self.input.clearButtonMode = UITextFieldViewModeAlways;
    self.input.clearsOnBeginEditing = YES;
    self.input.layer.cornerRadius = 5 * ScreenWidth;
    self.input.layer.masksToBounds = YES;
    self.input.delegate = self;
    self.input.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
    self.input.keyboardType = UIKeyboardTypePhonePad;
    self.input.placeholder = @"请输入验证码";
    self.input.textColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
    [self.view addSubview:self.input];
    //设置字体距离左边框的距离
    self.input.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10 * ScreenWidth, 0)];
    self.input.leftViewMode = UITextFieldViewModeAlways;
    
    //获取验证码
    self.get = [UIButton buttonWithType:UIButtonTypeCustom];
    self.get.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0 blue:140 / 255.0 alpha:1.0];
    self.get.layer.cornerRadius = 5 * ScreenWidth;
    self.get.layer.masksToBounds = YES;
    self.get.frame = CGRectMake(self.input.frame.origin.x + self.input.frame.size.width + 20 * ScreenWidth, self.input.frame.origin.y, 100 * ScreenWidth, 45 * ScreenHeigth);
    [self.get setTitleColor:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.get.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.get setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.get addTarget:self action:@selector(getAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.get];
    self.seconds = 61;
    
    
    //输入密码
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(self.input.frame.origin.x , self.input.frame.origin.y + self.input.frame.size.height + 20 * ScreenHeigth,  [UIScreen mainScreen].bounds.size.width - 40 * ScreenWidth, 45 * ScreenHeigth)];
    self.password.clearButtonMode = UITextFieldViewModeAlways;
    self.password.clearsOnBeginEditing = YES;
    self.password.layer.cornerRadius = 5 * ScreenWidth;
    self.password.layer.masksToBounds = YES;
    self.password.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
    //    self.password.keyboardType = UIKeyboardTypeNamePhonePad;
    self.password.placeholder = @"确认密码(6-16位)";
    self.password.textColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
    [self.view addSubview:self.password];
    //设置字体距离左边框的距离
    self.password.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10 * ScreenWidth, 0)];
    self.password.leftViewMode = UITextFieldViewModeAlways;
    
    
    
    
    //下一步
    UIButton *next = [UIButton buttonWithType:UIButtonTypeCustom];
    next.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0 blue:140 / 255.0 alpha:1.0];
    next.layer.cornerRadius = 10 * ScreenWidth;
    next.layer.masksToBounds = YES;
    next.frame = CGRectMake(la.frame.origin.x, self.password.frame.origin.y + self.password.frame.size.height + 30 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width - 40 * ScreenWidth, 45 * ScreenHeigth);
    [next setTitleColor:[UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    next.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    [next addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:next];
    
    
    
    //点击空白回收键盘
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
    [self.view addGestureRecognizer:ges];
    
    
    
    
    //添加全屏右滑动手势
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    
    
}


//限制手机输入十一位

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (self.text == textField)  //判断是否时我们想要限定的那个输入框
    {
        if (toBeString.length > 11) { //如果输入框内容大于20则弹出警告
            
            
            return NO;
            
        }
    }
    
    
    if (self.input == textField)  //判断是否时我们想要限定的那个输入框
    {
        if (toBeString.length > 6) { //如果输入框内容大于20则弹出警告
            
            
            return NO;
            
        }
    }
    
    
    
    
    
    return YES;
    
}



- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
}



//回收键盘
- (void)gesAction
{
    [self.password resignFirstResponder];
    [self.input resignFirstResponder];
    [self.check resignFirstResponder];
    [self.text resignFirstResponder];
    
    
    
}


//倒计时方法验证码实现倒计时60秒，60秒后按钮变换开始的样子
-(void)timerFireMethod:(NSTimer *)theTimer {
    if (_seconds == 1) {
        [theTimer invalidate];
        self.seconds = 60;
        [self.get setTitle:@"获取验证码" forState: UIControlStateNormal];
        [self.get setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.get setEnabled:YES];
    }else{
        self.seconds--;
        NSString *title = [NSString stringWithFormat:@"%ld",self.seconds];
        [self.get setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.get setEnabled:NO];
        [self.get setTitle:title forState:UIControlStateNormal];
    }
}

//如果登陆成功，停止验证码的倒数，
- (void)releaseTImer {
    if (_timer) {
        if ([self.timer respondsToSelector:@selector(isValid)]) {
            if ([_timer isValid]) {
                [_timer invalidate];
                self.seconds = 60;
            }
        }
    }
}

//获取验证码按钮
- (void)getAction:(UIButton *)sender
{
    //计时器
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
    
    
    
    
}
//下一步
- (void)nextAction:(UIButton *)sender
{
    
    if (self.text.text.length == 0 || self.password.text.length == 0 || self.input.text.length == 0) {
        
        
    } else {
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSDictionary *dict = @{@"mobile":self.text.text, @"password":self.password.text, @"mobileCode":self.input.text};
        
        [manager POST:Register_Api parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            
            NSInteger status = [[responseObject objectForKey:@"status_code"] integerValue];
            
            NSLog(@"%@", responseObject);
            //存userID到本地
            
            NSUserDefaults *userID = [NSUserDefaults standardUserDefaults];
            [userID setObject:[responseObject objectForKey:@"userId"] forKey:@"userId"];
            

            
            //存token到本地
            
            NSUserDefaults *userToken = [NSUserDefaults standardUserDefaults];
            [userToken setObject:[responseObject objectForKey:@"token"] forKey:@"token"];

            
            
            if (200 == status) {
                
                //添加水波动画
                CATransition *ansition = [CATransition animation];
                ansition.duration = 1.0;
                ansition.type = @"rippleEffect";
                [self.view.window.layer addAnimation:ansition forKey:nil];
                
                DataViewController *data = [[DataViewController alloc] init];
                [self.navigationController pushViewController:data animated:YES];
                
                
            }
            
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            
            NSLog(@"%@", error);
            
        }];
        
        
        
    }
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
